package pl.com.like.sequoia.web.beans.search;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import pl.com.like.sequoia.jms.client.producer.IJmsCompareMessageProducer;
import pl.com.like.sequoia.model.bio.BioObjectHandler;
import pl.com.like.sequoia.web.util.JsfUtils;

public class SearchObjectBean extends AbstractObjectBean implements Serializable {

	private static final long serialVersionUID = 1881296151696566257L;
	private IJmsCompareMessageProducer compareMessageProducer;
	private String objectName;
	private List<String> resultList;
	private List<String> parentList;
	private String parentId;
	private String compareId;
	private List<String> compareToList;
	private String removeName;
	private final static Logger log = LoggerFactory.getLogger(SearchObjectBean.class);
	private SearchPageControllerBean page = new SearchPageControllerBean();

	public String addToCompare() {
		if (compareToList == null) {
			compareToList = new ArrayList<String>();
		}
		parentId = JsfUtils.getParameter("paramId");
		if (parentId != null && !parentId.isEmpty()) {
			BioObjectHandler bio = getObject(parentId);
			if (bio != null) {
				if (parentList != null && parentList.contains(parentId)) {
					String msg = "selected protein have been already selected as parent of comparation. First change parent, then select this protein.";
					log.info(msg + " {}", parentId);
					JsfUtils.addWarnMessage(msg);
				} else {
					if (!compareToList.contains(parentId)) {
						compareToList.add(parentId);
					}
				}
			}
		} else {
			String msg = "any of objects have not been selected.";
			JsfUtils.addWarnMessage(msg);
			log.info(msg);
		}
		setParentId(null);
		return "";
	}

	public String assignParent() {
		if (parentList == null) {
			parentList = new ArrayList<String>();
		}
		parentId = JsfUtils.getParameter("paramId");
		if (parentId != null && !parentId.isEmpty()) {
			BioObjectHandler bio = getObject(parentId);
			if (bio != null) {
				parentList.clear();
				parentList.add(parentId);
				if (compareToList != null && compareToList.contains(parentId)) {
					compareToList.remove(parentId);
					String msg = "selected protein have been in comparation list.";
					JsfUtils.addWarnMessage(msg);
					log.info(msg + " {}", parentId);
				}
			} else {
				String msg = "protein have not been selected.";
				JsfUtils.addWarnMessage(msg);
				log.info(msg);
			}
		}
		setParentId(null);
		return "";
	}

	public String compareObjects() {
		String parent = null;
		String resultAction = "";
		if (parentList != null && (parentList.size() == 1)) {
			parent = parentList.get(0);
			if (compareToList != null && !compareToList.isEmpty()) {
				List<String> compareTo = compareToList;
				log.info("object for comparation have been sending for comparation. Parent: {}, compare to: {}",
						new Object[] { parent, compareTo });
				compareMessageProducer.compareObjects(parent, compareTo);
				resultAction = "compareQueue";
			} else {
				log.warn("any of object have not been selected for compare to.");
				JsfUtils.addErrorMessage("please select at least one object to compare to.");
			}
		} else {
			log.warn("any of object have not been selected as a parent of comparation");
			JsfUtils.addErrorMessage("please select parent for comparation.");
		}
		return resultAction;
	}

	public String getCompareId() {
		return compareId;
	}

	public IJmsCompareMessageProducer getCompareMessageProducer() {
		return compareMessageProducer;
	}

	public List<String> getCompareToList() {
		return compareToList;
	}

	public String getObjectName() {
		return objectName;
	}

	public SearchPageControllerBean getPage() {
		return page;
	}

	public String getParentId() {
		return parentId;
	}

	public List<String> getParentList() {
		return parentList;
	}

	public String getRemoveName() {
		return removeName;
	}

	public List<String> getResultList() {
		return resultList;
	}

	/**
	 * used for checking if minimal data are fulfilled for executing comparation.
	 * 
	 * @return TRUE if is selected parent and at least one object to compare
	 */
	public Boolean getValidComparation() {
		Boolean valid = Boolean.FALSE;
		if (parentList != null && (parentList.size() == 1)) {
			if (compareToList != null && !compareToList.isEmpty()) {
				valid = Boolean.TRUE;
			}
		}
		return valid;
	}

	public void removeElement() {
		parentId = JsfUtils.getParameter("paramId");
		if (parentId != null && compareToList != null) {
			compareToList.remove(parentId);
		}
	}

	public void removeParent() {
		parentList.clear();
	}

	public void reset() {
		parentList.clear();
		compareToList.clear();
		resultList.clear();
	}

	public void search() {
		if (objectName != null && !objectName.isEmpty()) {
			List<BioObjectHandler> resList = search(objectName, page.getPageNum(), 0);
			resultList = new ArrayList<String>();
			if (resList != null && !resList.isEmpty()) {
				for (BioObjectHandler bItem : resList) {
					resultList.add(bItem.getName());
				}
			}
		}
	}

	public void setCompareId(String compareId) {
		this.compareId = compareId;
	}

	public void setCompareMessageProducer(IJmsCompareMessageProducer compareMessageProducer) {
		this.compareMessageProducer = compareMessageProducer;
	}

	public void setCompareToList(List<String> compareToList) {
		this.compareToList = compareToList;
	}

	public void setObjectName(String objectName) {
		this.objectName = objectName;
	}

	public void setPage(SearchPageControllerBean page) {
		this.page = page;
	}

	public void setParentId(String parentId) {
		this.parentId = parentId;
	}

	public void setParentList(List<String> parentList) {
		this.parentList = parentList;
	}

	public void setRemoveName(String removeName) {
		this.removeName = removeName;
	}

	public void setResultList(List<String> resultList) {
		this.resultList = resultList;
	}

}
